package com.online.usermanagesystem.service.impl;

import com.online.usermanagesystem.dao.ConfigMapper;
import com.online.usermanagesystem.entity.Config;
import com.online.usermanagesystem.service.ConfigManageService;
import com.online.usermanagesystem.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

/**
 * 配置管理的处理类
 *
 * @create 2018-04-24 10:35
 */
@Component
public class ConfigManageServiceImpl implements ConfigManageService {

    @Autowired
    private ConfigMapper configMapper;

    @Override
    public Object get(String type, Integer owner) {
        if ((null == type) || (null == owner)) {
            return ResultUtil.BAD_REQUEST;
        }

        Config config;
        try {
            config = configMapper.query(type, owner);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (null == config) {
            return ResultUtil.NOT_FOUND;
        }

        return config;
    }

    @Override
    public Object list(Integer owner) {
        if (null == owner) {
            return ResultUtil.BAD_REQUEST;
        }

        try {
            return configMapper.list(owner);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }
    }

    @Override
    public Object set(Config config) {
        if ((null == config) || (null == config.getData())) {
            return ResultUtil.BAD_REQUEST;
        }

        try {
            if (null == config.getId()) {

                Integer count;
                try {
                    count = configMapper.count(config.getType(), config.getOwner());
                } catch (Exception e) {
                    e.printStackTrace();
                    return ResultUtil.INTERNAL_SERVER_ERROR;
                }

                if (0 != count) {
                    return ResultUtil.CONFLICT;
                }

                configMapper.insert(config);
            } else {
                configMapper.update(config.getId(), config.getData());
            }
        } catch (DuplicateKeyException e) {
            return ResultUtil.CONFLICT;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (null == config.getId()) {
            return config.getId();
        }

        return ResultUtil.OK;
    }

    @Override
    public Object del(Integer id) {
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        try {
            configMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }

    @Override
    public Object jsonGet(String type, Integer owner, String section) {
        if ((null == type) || (null == owner) || (null == section)) {
            return ResultUtil.BAD_REQUEST;
        }

        Config config;
        try {
            config = configMapper.jsonQuery(type, owner, String.format("$.%s", section));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (null == config) {
            return ResultUtil.NOT_FOUND;
        }

        return config;
    }

    @Override
    public Object jsonSet(Integer id, String section, String data) {
        if ((null == id) || (null == section) || (null == data)) {
            return ResultUtil.BAD_REQUEST;
        }

        try {
            Integer count = configMapper.jsonUpdate(id, String.format("$.%s", section), data);
            if (0 == count) {
                configMapper.jsonInsert(id, String.format("$.%s", section), data);
            }
        } catch (DuplicateKeyException e) {
            return ResultUtil.CONFLICT;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }

    @Override
    public Object jsonDel(Integer id, String section) {
        if ((null == id) || (null == section)) {
            return ResultUtil.BAD_REQUEST;
        }

        try {
            configMapper.jsonDelete(id, String.format("$.%s", section));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }
}
